//
//  IPVideoListController.swift
//  iPlayer
//
//  Created by jackcheng on 2017/11/9.
//  Copyright © 2017年 xiangyue. All rights reserved.
//

import UIKit
import IJKMediaFrameworkWithSSL
class IPVideoListController: IPViewController,UITableViewDelegate,UITableViewDataSource {
	
	lazy var tableView:UITableView = {
		let tableView:UITableView = UITableView.init(frame: CGRect(x: 0.0, y: kNavBarH, width: kScreenW, height: kScreenH - kFootGap - 49), style: .grouped)
		tableView.delegate = self
		tableView.dataSource = self
		tableView.register(UINib.init(nibName: "IPVideoCell", bundle: nil), forCellReuseIdentifier: "IPVideoCell")
		return tableView
	}()
	var videos = [IPVideoModel]()
	
	
	
	
    override func viewDidLoad() {
        super.viewDidLoad()
		self.title = "本地资源"
		self.view.addSubview(self.tableView)
		self.getLocalFiles()
        // Do any additional setup after loading the view.
    }
	
	func getLocalFiles() {
		let fileManager = FileManager.default
		let documentPaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, .userDomainMask, true)
		let documentDir = documentPaths.first
		
		do {
			let fileList = try fileManager.contentsOfDirectory(atPath: documentDir!)
			for fileName in fileList {
				if fileName.hasSuffix("rm")||fileName.hasSuffix("rmvb")||fileName.hasSuffix("wmv")||fileName.hasSuffix("avi")||fileName.hasSuffix("mp4")||fileName.hasSuffix("3gp")||fileName.hasSuffix("mkv")||fileName.hasSuffix("mov")||fileName.hasSuffix("flv") {
					let model:IPVideoModel = IPVideoModel()
					model.name = fileName
					model.filePath = documentDir! + "/" + fileName
					do {
						let atrr = try fileManager.attributesOfItem(atPath: model.filePath)
						model.bytesCount = atrr[FileAttributeKey.size] as! Int
					}catch let error {
						
					}
					let videoInfo = IJKFFMoviePlayerController.getFirtThumbnailImageAtFirtSencond(withUrl: model.filePath.cString(using: .utf8)) as! [String:Any]
					model.thumbImage = videoInfo["image"] as! UIImage
					self.videos.append(model)
					self.tableView.reloadData()
				}
				
			}
		} catch let error {
			print("获取失败:"+error.localizedDescription)
		}

		
		
	}
	
	func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return self.videos.count
	}
	
	func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
		return 60
	}
	
	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let video:IPVideoModel = self.videos[indexPath.row]
		let cell:IPVideoCell = tableView.dequeueReusableCell(withIdentifier: "IPVideoCell") as! IPVideoCell
		cell.model = video
		return cell
	}
	
	func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
		 tableView.deselectRow(at: indexPath, animated: true)
//		let video:IPVideoModel = self.videos[indexPath.row]
//		let player = IPlayerController.init(urls: [video.filePath])
//		self.present(player, animated: true, completion: nil)
		let web:IPWebViewPlayer = IPWebViewPlayer.init(urlString: "http://www.iqiyi.com/")
		web.hidesBottomBarWhenPushed = true
		self.navigationController?.pushViewController(web, animated: true)
	}

	
	
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
